package com.qdsg.ysg.doctor.manager;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.Toast;


import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.ui.widget.CircleLoadingView;
import com.qdsg.ysg.doctor.util.ToastUtil;


/**
 * Created by HP on 2018/7/20.
 */

public class DialogManager2 {
    private Dialog mDialog;

    private CircleLoadingView mIcon;
    private ImageView mCancel,mIcon_VoiceLeft,mIcon_VoiceRight;


    private Context mContext;

    public DialogManager2(Context context) {
        mContext = context;
    }

    public void showRecordingDialog() {
        mDialog = new Dialog(mContext, R.style.Theme_AudioDialog);
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.dialog_recorder2, null);
        Window window = mDialog.getWindow();
        if (window != null) {
            window.setGravity(Gravity.BOTTOM);
            window.getDecorView().setMinimumWidth(mContext.getResources().getDisplayMetrics().widthPixels);
        }
        mDialog.setContentView(view);
        mIcon = view.findViewById(R.id.id_recorder_dialog_icon);
        mCancel = view.findViewById(R.id.id_recorder_dialog_cancel);
        mIcon_VoiceLeft = view.findViewById(R.id.mIcon_VoiceLeft);
        mIcon_VoiceRight =view.findViewById(R.id.mIcon_VoiceRight);
        mDialog.show();
    }

    //正在播放时的状态
    public void recording() {
        if (mDialog != null && mDialog.isShowing()) {
            mIcon.setVisibility(View.VISIBLE);
            mIcon.setImageResource(R.mipmap.icon_speaking);
            mCancel.setImageResource(R.mipmap.icon_del_grey);
        }
    }

    //想要取消
    public boolean wantToCancel() {
        if (mDialog != null && mDialog.isShowing()) {
            mIcon.setVisibility(View.VISIBLE);
            mCancel.setImageResource(R.mipmap.icon_del_red);
        }
        return false;
    }

    //录音时间太短
    public void tooShort() {
        if (mDialog != null && mDialog.isShowing()) {
            mIcon.setVisibility(View.VISIBLE);
            mIcon.setImageResource(R.mipmap.voice_to_short);
            ToastUtil.showToast( mContext, "录音时间太短" );
        }
    }

    //关闭dialog
    public void dimissDialog() {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
            mDialog = null;
        }
    }

    /**
     * 通过level更新voice上的图片
     *
     * @param level
     */
    public void updateVoiceLevel(int level) {
        if (mDialog != null && mDialog.isShowing()) {

            int resIdLeft = mContext.getResources().getIdentifier("volumel" + level, "mipmap", mContext.getPackageName());
            int resIdRight = mContext.getResources().getIdentifier("volumer" + level, "mipmap", mContext.getPackageName());

            mIcon_VoiceLeft.setImageResource(resIdLeft);
            mIcon_VoiceRight.setImageResource(resIdRight);


        }
    }



}
